package lj.courseinterect.entity;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.persistence.*;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

@javax.persistence.Entity
public class Student implements Entity, UserDetails
{
    public enum StuState {
        NORMAL, // 正常
        GRADUATED, // 已毕业
        OTHERS // 其他
    }

    /**
     * 主键id
     */
	@Id
	@GeneratedValue
    @Column(name = "stu_id")
	private Long stuId;

    /**
     * 学号
     */
    @Column(name = "stu_num", updatable = false, unique = true, nullable = false)
    private String stuNum;

    /**
     * 专业id
     */
    @Column(name = "major_id", nullable = false)
    private int majorId;

    /**
     * 姓名
     */
    @Column(name = "stu_name", nullable = false)
    private String stuName;

    /**
     * 密码
     */
	@Column(name = "stu_pwd", length = 80, nullable = false)
	private String stuPwd;

    /**
     * 权限集
     */
	@ElementCollection(fetch = FetchType.EAGER)
	private Set<String> roles = new HashSet<String>();

    /**
     * 学生状态
     */
    @Enumerated(EnumType.ORDINAL)
    @Column(name = "stu_state", columnDefinition = "int(11) default 0")
    private StuState stuState;

	public Student()
	{
		/* Reflection instantiation */
	}


	public Student(String stuNum, String stuPwdHash)
	{
		this.stuNum = stuNum;
		this.stuPwd = stuPwdHash;
	}

    public Long getStuId() {
        return stuId;
    }

    public void setStuId(Long stuId) {
        this.stuId = stuId;
    }

    public String getStuNum() {
        return stuNum;
    }

    public void setStuNum(String stuNum) {
        this.stuNum = stuNum;
    }

    public int getMajorId() {
        return majorId;
    }

    public void setMajorId(int majorId) {
        this.majorId = majorId;
    }

    public String getStuName() {
        return stuName;
    }

    public void setStuName(String stuName) {
        this.stuName = stuName;
    }

    public String getStuPwd() {
        return stuPwd;
    }

    public void setStuPwd(String stuPwd) {
        this.stuPwd = stuPwd;
    }

    public StuState getStuState() {
        return stuState;
    }

    public void setStuState(StuState stuState) {
        this.stuState = stuState;
    }

	public Set<String> getRoles()
	{
		return this.roles;
	}


	public void setRoles(Set<String> roles)
	{
		this.roles = roles;
	}


	public void addRole(String role)
	{
		this.roles.add(role);
	}


	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		Set<String> roles = this.getRoles();

		if (roles == null) {
			return Collections.emptyList();
		}

		Set<GrantedAuthority> authorities = new HashSet<GrantedAuthority>();
		for (String role : roles) {
			authorities.add(new SimpleGrantedAuthority(role));
		}

		return authorities;
	}

    @Override
    public String getPassword() {
        return this.stuPwd;
    }

    @Override
    public String getUsername() {
        return this.stuNum;
    }


	@Override
	public boolean isAccountNonExpired()
	{
		return true;
	}


	@Override
	public boolean isAccountNonLocked()
	{
		return true;
	}


	@Override
	public boolean isCredentialsNonExpired()
	{
		return true;
	}


	@Override
	public boolean isEnabled()
	{
		return true;
	}

}